using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillTrigger_CustomProperty : SkillTrigger
    {
        protected override ESkillTriggerType triggerType => ESkillTriggerType.CustomProperty;

        private ECustomProperty propertyType;
        private int value;
        private string customKey;
        private bool onlySelf;

        protected override void OnInitialize(Skill rSkill, TriggerConfig rConfig)
        {
            base.OnInitialize(rSkill, rConfig);
            if (rConfig.customConfig is SkillTrigger_CustomProperty_ConfigData rConfigData)
            {
                this.propertyType = (ECustomProperty)rConfigData.propertyType;
                this.value = rConfigData.value;
                this.customKey = rConfigData.customKey;
                this.onlySelf = rConfigData.onlySelf;
            }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            this.AddListener<ECustomProperty, string, Unit>(EBattleTriggerType.Battle_SetCustomProperty, this.OnEvent);
        }

        private void OnEvent(ECustomProperty rType, string customKey, Unit rTarget)
        {
            if (!this.onlySelf || rTarget.GUID == this.executer.GUID)
            {
                var rCustomComp = rTarget.GetComponent<CustomPropertyComponent>(ETComponentType.CustomPropertyComponent);
                if (rCustomComp == null)
                {
                    return;
                }
                if (string.IsNullOrEmpty(this.customKey))
                {
                    if (this.propertyType != rType || (this.value > 0 && rCustomComp.GetProperty<int>(rType) < this.value))
                    {
                        return;
                    }
                }
                else
                {
                    if (this.customKey != customKey || (this.value > 0 && rCustomComp.GetProperty(this.customKey) < this.value))
                    {
                        return;
                    }
                }


                var rVirInput = ClassPool.Get<SelectParam>();
                rVirInput.unit = this.skill.executer;
                rVirInput.pos = rVirInput.unit.pos;
                this.OnTrigger(rVirInput);
            }
        }

        public override void OnReset()
        {
            base.OnReset();
            this.propertyType = ECustomProperty.Min;
            this.value = default;
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }

#if UNITY_EDITOR
        public override string ToString()
        {
            return $"自身自定义Int参数[{this.propertyType}]修改 >= {this.value}";
        }
#endif
    }
}